//
//  LCWNetworkingTools.swift
//  MyWB
//
//  Created by 李成万 on 16/6/26.
//  Copyright © 2016年 李成万. All rights reserved.
//

import UIKit
import AFNetworking
enum Method : String{
    case GET = "GET"
    case POST = "POST"
}

class LCWNetworkingTools: AFHTTPSessionManager {

    typealias SuccessClosure = (response:AnyObject?)->()
    typealias FailureClosure = (error:NSError)->()
    static let sharedTools : LCWNetworkingTools = {
        let tools = LCWNetworkingTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    //封装的网络工具类
    /**
    封装的网络工具类
    
    - parameter method:     method description
    - parameter URLString:  URLString description
    - parameter parameters: parameters description
    - parameter success:    success description
    - parameter failure:    failure description
    */
    func request(method:Method,URLString: String,parameters: AnyObject?,success:SuccessClosure,failure:FailureClosure){
        if method == .GET{
            self.GET(URLString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
        }else{
            self.POST(URLString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
                success(response: res)
                }, failure: { (_, err) -> Void in
                    failure(error: err)
            })
        }
    }
}

extension LCWNetworkingTools{
    func oAuthLoadRequest(code:String,success: SuccessClosure, failure: FailureClosure){
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        
        let param = [
            "client_id" : APPKEY,
            "client_secret" : APPSECRET,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : HUIDIAOYE
        ]
        
        self.request(.POST, URLString: urlStr, parameters: param, success: success, failure: failure)
    }
    
    func oAuthLoadUserInfo(account:LCWUserAccount,success:SuccessClosure,failure:FailureClosure){
        let urlStr = "https://api.weibo.com/2/users/show.json"
        
        let param = [
            "access_token": account.access_token!,
            "uid":account.uid!
        ]
        
        self.request(.GET, URLString: urlStr, parameters: param, success: success, failure: failure)
    }
}

extension LCWNetworkingTools{
    //请求首页数据封装
    func homeLoadData(since_Id:Int64,max_Id:Int64,success:SuccessClosure,failure:FailureClosure){
        let urlStr = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let param = [
            "access_token":LCWUserAccountTools.sharedTool.accessToken!,
            "since_id":"\(since_Id)",
            "max_id":"\(max_Id)"
            
        ]
        self.request(.GET, URLString: urlStr, parameters: param, success: success, failure: failure)
    }
}

extension LCWNetworkingTools{
    /**
    *  发文字微博方法封装
    */
    func update(content:String,success:SuccessClosure,failure:FailureClosure){
        let urlStr = "https://api.weibo.com/2/statuses/update.json"
        let param = [
            /**
            *  access_token	true	string	采用OAuth授权方式为必填参数，OAuth授权后获得。
            status
            */
           "access_token":LCWUserAccountTools.sharedTool.accessToken!,
            "status":content
        ]
        
        self.request(.POST, URLString: urlStr, parameters: param, success: success, failure: failure)

    }
    func upload(content:String,imageArr:[UIImage],success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        /**
        https://upload.api.weibo.com/2/statuses/upload.json

        access_token	false	string	采用OAuth授权方式为必填参数，其他授权方式不需要此参数，OAuth授权后获得。
        status
        */
        
        let urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"
        let param = [
            "access_token":LCWUserAccountTools.sharedTool.accessToken!,
            "status":content
        ]
        
        self.POST(urlStr, parameters: param, constructingBodyWithBlock: { (formData) -> Void in
            
            for image in imageArr{
                let data = UIImagePNGRepresentation(image)!
                formData.appendPartWithFileData(data, name: "pic", fileName: "123", mimeType: "application/octet-stream")
            }
            
            }, progress: nil, success: { (_, response) -> Void in
                success(response:response)
            }) { (_, error) -> Void in
                failure(error: error)
        }
    }
    
}